using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace RockBus.ServiceModel.Diagnostics
{
    public class MessageLoggingBehavior : IEndpointBehavior
    {
        private readonly MessageLoggingMessageInspector _inspector;

        public MessageLoggingBehavior(MessageLoggingStagesType messageLoggingStages)
        {
            this._inspector = new MessageLoggingMessageInspector(messageLoggingStages);
        }

        public MessageLoggingBehavior(MessageLoggingStagesType messageLoggingStages, int maxBufferSize)
        {
            this._inspector = new MessageLoggingMessageInspector(messageLoggingStages, maxBufferSize);
        }

        #region IEndpointBehavior Members

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(this._inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this._inspector);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        #endregion IEndpointBehavior Members
    }
}